package com.diu.domain.strategy.service.rule.filter.impl;

import com.diu.domain.strategy.model.entity.RuleActionEntity;
import com.diu.domain.strategy.model.entity.RuleMatterEntity;
import com.diu.domain.strategy.model.vo.RuleLogicCheckTypeVO;
import com.diu.domain.strategy.repository.IStrategyRepository;
import com.diu.domain.strategy.service.annotation.LogicStrategy;
import com.diu.domain.strategy.service.rule.filter.ILogicFilter;
import com.diu.domain.strategy.service.rule.filter.factory.DefaultLogicFactory;
import com.diu.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author #diu
 * @description #黑名单过滤器
 * @create #2025/10/10 15:46
 */
@Slf4j
@Component
@LogicStrategy(logicMode = DefaultLogicFactory.LogicModel.RULE_BLACKLIST)
public class RuleBlackListLogicFilter implements ILogicFilter<RuleActionEntity.RaffleBeforeEntity> {
    @Resource
    private IStrategyRepository repository;
    @Override
    public RuleActionEntity<RuleActionEntity.RaffleBeforeEntity> filter(RuleMatterEntity ruleMatterEntity) {
        log.info("黑名单过滤器触发，userId:{} strategyId:{} ruleModel:{}", ruleMatterEntity.getUserId(), ruleMatterEntity.getStrategyId(), ruleMatterEntity.getRuleModel());
        String userId = ruleMatterEntity.getUserId();

        String ruleValue = repository.queryStrategyRuleValue(ruleMatterEntity.getStrategyId(), ruleMatterEntity.getAwardId(), ruleMatterEntity.getRuleModel());
        if(ruleValue != null && !ruleValue.isEmpty()){
            String[] splitRuleValues = ruleValue.split(Constants.COLON);
            Integer awardId = Integer.valueOf(splitRuleValues[0]);
            for(String rule : splitRuleValues[1].split(Constants.SPLIT)){
                if(userId.equals(rule)){
                    log.info("用户命中黑名单，无法参与抽奖，userId:{} strategyId:{}", userId, ruleMatterEntity.getStrategyId());
                    return RuleActionEntity.<RuleActionEntity.RaffleBeforeEntity>builder()
                            .code(RuleLogicCheckTypeVO.TAKE_OVER.getCode())
                            .info(RuleLogicCheckTypeVO.TAKE_OVER.getInfo())
                            .ruleModel(DefaultLogicFactory.LogicModel.RULE_BLACKLIST.getCode())
                            .data(RuleActionEntity.RaffleBeforeEntity.builder()
                                    .strategyId(ruleMatterEntity.getStrategyId())
                                    .awardId(awardId)
                                    .beforeState("no")
                                    .build())
                            .build();
                }
            }
        }

        return RuleActionEntity.<RuleActionEntity.RaffleBeforeEntity>builder()
                .code(RuleLogicCheckTypeVO.ALLOW.getCode())
                .info(RuleLogicCheckTypeVO.ALLOW.getInfo())
                .ruleModel(DefaultLogicFactory.LogicModel.RULE_BLACKLIST.getCode())
                .data(RuleActionEntity.RaffleBeforeEntity.builder()
                        .strategyId(ruleMatterEntity.getStrategyId())
                        .beforeState("yes")
                        .build())
                .build();
    }
}
